package controllers.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Oplog;
import rbac.models.PrivilegeType;
import rbac.models.Role;
import rbac.models.User;

import com.et.mvc.JsonView;
import com.et.mvc.View;

import controllers.ApplicationController;

public class RoleController extends ApplicationController {
	public void index() throws Exception {
		List<PrivilegeType> types = PrivilegeType.findAll(PrivilegeType.class,
				null, null, "sort");
		request.setAttribute("types", types);
	}

	public View getptRoles(int page, int rows) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", Role.count(Role.class, "description not like '%系统%' and description not like '%超级%'", null));
		result.put("rows", Role.findAll(Role.class, "description not like '%系统%' and description not like '%超级%'", null, "sort", rows,
				(page - 1) * rows));
		return new JsonView(result);
	}
	
	public View getRoles(int page, int rows) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", Role.count(Role.class, null, null));
		result.put("rows", Role.findAll(Role.class, null, null, "sort", rows,
				(page - 1) * rows));
		return new JsonView(result);
	}

	public View getRoleList() throws Exception {
		List<Role> items = Role.findAll(Role.class, null, null, "sort");
		return new JsonView(items);
	}

	public View save(Role role, String[] pids) throws Exception {
		Integer sort = (Integer) Role.maximum(Role.class, "sort", null, null);
		if (sort == null) {
			sort = 1;
		} else {
			sort += 1;
		}
		role.setSort(sort);
		if (pids == null) {
			role.setPrivilegeids(null);
		} else {
			role.setPrivilegeids(join(pids));
		}
		role.save();
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建角色，内容为：" + new JsonView(role));									

		View view = new JsonView("success:true");
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}

	public View update(int id, Map<String, Object> params, String[] pids)
			throws Exception {
		Role role = Role.find(Role.class, id);
		JsonView olditem= new JsonView(role);
		role = Role.updateModel(role, params);
		if (pids == null) {
			role.setPrivilegeids(null);
		} else {
			role.setPrivilegeids(join(pids));
		}
		role.save();
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改角色，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(role));										

		View view = new JsonView("success:true");
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}

	public View destroy(int id) throws Exception {
		Role item = Role.find(Role.class, id);
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除角色，内容为：" + new JsonView(item));									

		item.destroy();
		return new JsonView("success:true");
	}

	public View move(int id, String dir) throws Exception {
		Role item = Role.find(Role.class, id);
		Role target = null;
		if (dir.equals("down")) {
			target = Role.findFirst(Role.class, "sort>?", new Object[] { item
					.getSort() }, "sort asc");
		} else {
			target = Role.findFirst(Role.class, "sort<?", new Object[] { item
					.getSort() }, "sort desc");
		}
		if (target != null) {
			Integer tmp = target.getSort();
			target.setSort(item.getSort());
			target.save();
			item.setSort(tmp);
			item.save();
		}
		return new JsonView("success:true");
	}
}
